package com.mflex.mwms.activity;


import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.MoveOnShelfAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.MoveOnRequest;
import com.mflex.mwms.model.ShelfOnLocation;
import com.mflex.mwms.model.enumeration.CheckAction;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MoveOnShelfOnActivity extends BaseActivity {
    @BindView(R.id.stv_ShelfLocation)
    EditText stvShelfLocation;

    @BindView(R.id.stv_Qty)
    EditText stvQty;

    @BindView(R.id.stv_Pkg)
    EditText stvPkg;

    @BindView(R.id.button_clear)
    Button buttonClear;

    @BindView(R.id.button_save)
    Button buttonSave;

    @BindView(R.id.lvPackList)
    ListView lvPackList;

    MiniLoadingDialog mMiniLoadingDialog;

    List<ShelfOnLocation> shelfOnLocationList;

    private int boxQty = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move_on_shelf_on);

        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");
        stvQty.setFocusable(false);

        stvShelfLocation.setOnKeyListener(this);
        stvPkg.setOnKeyListener(this);
        buttonClear.setOnClickListener(this);
        buttonSave.setOnClickListener(this);

        shelfOnLocationList = new ArrayList<>();
        MoveOnShelfAdapter moveOnShelfAdapter = new MoveOnShelfAdapter(MoveOnShelfOnActivity.this, shelfOnLocationList);
        lvPackList.setAdapter(moveOnShelfAdapter);
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_ShelfLocation:
                    checkShelfLocation(view);

                    break;
                case R.id.stv_Pkg:
                    checkPkg(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_save:
                saveButtonMd(view);
                break;
            default:
                break;
        }
    }

    private void clearButtonMd(View v) {
        stvShelfLocation.setText("");
        stvQty.setText("");
        stvPkg.setText("");
        shelfOnLocationList = new ArrayList<>();
        MoveOnShelfAdapter moveOnShelfAdapter = new MoveOnShelfAdapter(MoveOnShelfOnActivity.this, shelfOnLocationList);
        lvPackList.setAdapter(moveOnShelfAdapter);
        boxQty = 0;
        stvShelfLocation.postDelayed(new Runnable() {
            @Override
            public void run() {
                stvShelfLocation.requestFocus();
            }
        }, 200);
    }

    private void saveButtonMd(View v) {
        if (StringUtils.isEmpty(stvShelfLocation.getText().toString())) {
            XToastUtils.error("上架库位未输入", 3000);
            MessageUtils.errorMsgAndVoice(MoveOnShelfOnActivity.this, "上架库位未输入");
            return;
        }

        List<String> list = new ArrayList<>();
        for (ShelfOnLocation shelfOnLocation : shelfOnLocationList) {
            list.add(shelfOnLocation.getBoxNumber());
        }
        MoveOnRequest moveOnRequest = new MoveOnRequest();
        moveOnRequest.setWarehouse(MMKVUtils.getString("Warehose", ""));
        moveOnRequest.setShelfLocation(stvShelfLocation.getText().toString());
        moveOnRequest.setBoxNumber(list);

        mMiniLoadingDialog.show();
        XHttp.post("api/ShelfOn/MoveOnPda")
                .upJson(JsonUtil.toJson(moveOnRequest))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        clearButtonMd(v);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MessageUtils.errorMsgAndVoice(MoveOnShelfOnActivity.this, e);
                    }
                }) {
                });
    }

    private void checkPkg(View v) {
        if (StringUtils.isEmpty(stvShelfLocation.getText().toString())) {
//            XToastUtils.error("上架库位为未输入");
            MessageUtils.errorMsgAndVoice(MoveOnShelfOnActivity.this, "上架库位为未输入");
            stvPkg.setText("");
            stvPkg.requestFocus();
            return;
        }
        boolean flag = false;
        for (ShelfOnLocation shelfOnLocation : shelfOnLocationList) {
            if (StringUtils.equals(shelfOnLocation.getBoxNumber(), stvPkg.getText().toString())) {
                flag = true;
                break;

            }
        }
        if (flag) {
//            new MaterialDialog.Builder(v.getContext())
//                    .iconRes(R.drawable.icon_warning)
//                    .title(R.string.tip_warning)
//                    .content("该包已在列表中")
//                    .positiveText(R.string.lab_submit)
//                    .show();
            MessageUtils.errorMsgAndVoice(MoveOnShelfOnActivity.this, "该包已在列表中");
            stvPkg.setText("");
            stvPkg.requestFocus();
            return;
        }

        mMiniLoadingDialog.show();
        XHttp.get("api/ShelfOn/CheckShelfOn")
                .params("BoxNumber", stvPkg.getText().toString())
                .params("Warehouse", MMKVUtils.getString("Warehose", ""))
                .params("checkAction", CheckAction.MOVEON.getCode())
                .params("LocationName", stvShelfLocation.getText().toString())
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<ShelfOnLocation>, ShelfOnLocation>(new SimpleCallBack<ShelfOnLocation>() {
                    @Override
                    public void onSuccess(ShelfOnLocation response) {
                        mMiniLoadingDialog.dismiss();
                        response.setBoxNumber(stvPkg.getText().toString());
                        shelfOnLocationList.add(response);
                        MoveOnShelfAdapter moveOnShelfAdapter = new MoveOnShelfAdapter(MoveOnShelfOnActivity.this, shelfOnLocationList);
                        lvPackList.setAdapter(moveOnShelfAdapter);
                        boxQty = boxQty + 1;
                        stvQty.setText(String.valueOf(boxQty));
                        stvPkg.setText("");
                        stvPkg.requestFocus();
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvPkg.setText("");
                        stvPkg.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(MoveOnShelfOnActivity.this, e);
                    }
                }) {
                });
    }

    private void checkShelfLocation(View v) {
        mMiniLoadingDialog.show();
        XHttp.get("api/ShelfOn/CheckShelfOnLocation")
                .params("Location", stvShelfLocation.getText().toString())
                .params("Warehouse", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvShelfLocation.setText("");
                        stvShelfLocation.requestFocus();
                        MessageUtils.errorMsgAndVoice(MoveOnShelfOnActivity.this, e);
                    }
                }) {
                });
    }
}